<template>
  <div class="app-container">
    <h1>Vue3 响应式基础实例</h1>
    
    <!-- 关卡1：ref 示例 -->
    <section class="level level-1">
      <h2>🌟 关卡1：ref - 让原始值变成响应式小精灵</h2>
      <div class="game">
        <h3>勇者血量：{{ hp }}</h3>
        <button @click="attack" :disabled="attacking" class="attack-btn">
          {{ attacking ? '攻击中...' : '发起攻击' }}
        </button>
      </div>
    </section>

    <!-- 关卡2：reactive 示例 -->
    <section class="level level-2">
      <h2>🌟 关卡2：reactive - 让对象一次响应到底</h2>
      <div class="hero-card">
        <h3>{{ name }} Lv.{{ level }}</h3>
        <p>技能列表：{{ hero.skills.join('、') }}</p>
        <button @click="levelUp" class="level-btn">打怪升级</button>
      </div>
    </section>

    <!-- 关卡3：computed 示例 -->
    <section class="level level-3">
      <h2>🌟 关卡3：computed - 声明式衍生数据</h2>
      <div class="shopping-cart">
        <p>单价：{{ price }} × 数量：{{ quantity }} × 折扣：{{ discount }}</p>
        <h4>应付：{{ total.toFixed(2) }} 元</h4>
        <button @click="add" class="add-btn">再来一杯</button>
      </div>
    </section>

    <!-- 关卡4：watch & watchEffect 示例 -->
    <section class="level level-4">
      <h2>🌟 关卡4：watch & watchEffect - 侧效应侦察兵</h2>
      
      <div class="watch-demo">
        <h3>watch 示例</h3>
        <p>当前ID：{{ id }}</p>
        <p>数据：{{ JSON.stringify(data) }}</p>
        <button @click="nextId" class="next-btn">下一个ID</button>
      </div>

      <div class="watch-effect-demo">
        <h3>watchEffect 示例</h3>
        <p>角色坐标：({{ x }}, {{ y }})</p>
        <div class="controls">
          <button @click="x++" class="move-btn">X+1</button>
          <button @click="y++" class="move-btn">Y+1</button>
        </div>
        <p class="tip">查看控制台输出</p>
      </div>
    </section>

    <!-- 关卡5：可写的计算属性示例 -->
    <section class="level level-5">
      <h2>🌟 关卡5：可写的计算属性</h2>
      <div class="name-demo">
        <p>姓：{{ firstName }}</p>
        <p>名：{{ lastName }}</p>
        <p>全名：{{ fullName }}</p>
        <input v-model="fullName" placeholder="输入全名" class="name-input">
        <button @click="setFullName" class="set-name-btn">设置为"王 五"</button>
      </div>
    </section>

    <!-- 关卡6：计算属性缓存特性示例 -->
    <section class="level level-6">
      <h2>🌟 关卡6：计算属性缓存 vs 方法</h2>
      <div class="cache-demo">
        <p>计数：{{ count }}</p>
        <p>倍数：{{ multiplier }}</p>
        
        <div class="results">
          <div>
            <h4>计算属性（有缓存）：</h4>
            <p>{{ computedResult }}</p>
            <p>{{ computedResult }}</p>
          </div>
          <div>
            <h4>方法（无缓存）：</h4>
            <p>{{ methodResult() }}</p>
            <p>{{ methodResult() }}</p>
          </div>
        </div>
        
        <button @click="count++" class="increment-btn">计数+1</button>
        <p class="tip">查看控制台输出对比</p>
      </div>
    </section>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, computed, watch, watchEffect, toRefs } from 'vue'

// 关卡1: ref 示例
const hp = ref(100)          // 英雄血量
const attacking = ref(false) // 是否正在攻击

const attack = () => {
  attacking.value = true
  setTimeout(() => {
    hp.value -= 20
    attacking.value = false
  }, 500)
}

// 关卡2: reactive 示例
interface Hero {
  name: string
  level: number
  skills: string[]
}

const hero = reactive<Hero>({
  name: 'Vue 法师',
  level: 1,
  skills: ['火球术']
})

// 安全解构（保持响应）
const { name, level } = toRefs(hero)

const levelUp = () => {
  level.value++
  if (level.value === 3) hero.skills.push('冰冻术')
  if (level.value === 5) hero.skills.push('闪电链')
}

// 关卡3: computed 示例
const price = ref(25)
const quantity = ref(2)
const discount = ref(0.8)

// 计算购物车总价
const total = computed<number>(() => {
  return price.value * quantity.value * discount.value
})

const add = () => quantity.value++

// 关卡4: watch & watchEffect 示例
const id = ref(1)
const data = ref<any>(null)

// 监听 id 变化，拉取新数据
watch(id, async (newId, oldId) => {
  console.log(`ID 从 ${oldId} → ${newId}`)
  // 模拟API请求
  data.value = { id: newId, title: `数据项 ${newId}`, completed: false }
}, { immediate: true })

const nextId = () => id.value++

// watchEffect 示例
const x = ref(0)
const y = ref(0)

// 只要 x 或 y 变，就打印最新坐标
watchEffect(() => {
  console.log(`角色坐标：(${x.value}, ${y.value})`)
})

// 关卡5: 可写的计算属性示例
const firstName = ref('张')
const lastName = ref('三')

const fullName = computed({
  get() {
    return firstName.value + ' ' + lastName.value
  },
  set(newValue: string) {
    const names = newValue.split(' ')
    firstName.value = names[0]
    lastName.value = names.length > 1 ? names[names.length - 1] : ''
  }
})

const setFullName = () => {
  fullName.value = '王 五'
}

// 关卡6: 计算属性缓存 vs 方法
const count = ref(1)
const multiplier = ref(2)

// 计算属性（有缓存）
const computedResult = computed(() => {
  console.log('计算属性执行')
  return count.value * multiplier.value
})

// 方法（无缓存，每次调用都执行）
const methodResult = () => {
  console.log('方法执行')
  return count.value * multiplier.value
}
</script>

<style scoped>
.app-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

h1 {
  text-align: center;
  color: #42b983;
  margin-bottom: 40px;
}

.level {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 30px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.level h2 {
  color: #333;
  border-bottom: 2px solid #42b983;
  padding-bottom: 10px;
  margin-bottom: 20px;
}

/* 关卡1 样式 */
.game {
  text-align: center;
  padding: 20px;
}

.attack-btn {
  background: #ff6b6b;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.attack-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

/* 关卡2 样式 */
.hero-card {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.level-btn {
  background: #42b983;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

/* 关卡3 样式 */
.shopping-cart {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.add-btn {
  background: #4285f4;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

/* 关卡4 样式 */
.level-4 > div {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.watch-demo, .watch-effect-demo {
  flex: 1;
  min-width: 300px;
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.next-btn, .move-btn {
  background: #673ab7;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-top: 10px;
}

.move-btn {
  margin-right: 10px;
}

/* 关卡5 样式 */
.name-demo {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.name-input {
  width: 100%;
  padding: 8px;
  margin: 10px 0;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.setName-btn {
  background: #ff9800;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

/* 关卡6 样式 */
.cache-demo {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.results {
  display: flex;
  gap: 20px;
  margin: 20px 0;
}

.results > div {
  flex: 1;
  padding: 15px;
  background: #f8f9fa;
  border-radius: 4px;
}

.increment-btn {
  background: #2196f3;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
}

.tip {
  color: #666;
  font-size: 14px;
  margin-top: 10px;
  font-style: italic;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .level-4 > div, .results {
    flex-direction: column;
  }
}
</style>